import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/14 15:02
 * @description 动态规划
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] jo = {11,111,22,222,33,333,44,444};
        solution.minDifficulty(jo, 6);
    }

    public int minDifficulty(int[] job, int d) {
        if (d > job.length) {
            return -1;
        }
        int[][] dp = new int[d][job.length];
        // 预处理区间i～j的难度值
        int[][] cost = new int[job.length][job.length];
        for (int i = 0; i < job.length; i++) {
            int max = job[i];
            for (int j = i; j < job.length; j++) {
                max = Math.max(max, job[j]);
                cost[i][j] = max;
            }
        }
        int INF = 100000000;
        for (int i = 0; i < d; i++) {
            Arrays.fill(dp[i], INF);
        }
        for (int i = 0; i < job.length; i++) {
            dp[0][i] = cost[0][i];
        }
        for (int i = 1; i < d; i++) {
            for (int j = i; j < job.length; j++) {
                for (int k = 0; k < j; k++) {
                    dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + cost[k + 1][j]);
                }
            }
        }
        return dp[d - 1][job.length - 1];
    }
}
